#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
#include<map>
using namespace std;

class Solution {
public:
    int subarraysWithKDistinct(vector<int>& A, int K) {
        //滑动数组问题
        //使用map来确定区间中的数
        
        int result=0;
        for(int i=0;i<A.size();++i){
            map<int,int> M;
            int left = i;
            int right = i;
            
            while(right<A.size()&&M.size()<=K){
                ++M[A[right]];
                if(M.size()==K){
                    ++result;
                }
                if(M.size()>K){
                    break;
                }
                ++right;
            }
        }
        return result;
    }
};

int main(){
    Solution S;
    vector<int> A= {1,2,1,3,4};
    cout<<S.subarraysWithKDistinct(A,3)<<endl;
}